package com.education.module.system.sysRole;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.education.domain.SysRole;
import com.education.framework.base.BaseServices;
import com.education.framework.dao.IDao;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;

@Service
public class SysRoleServices extends BaseServices implements IDao<SysRole>{

	@Override
	public List<SysRole> find(SearchParams searchParams, Page page) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,role_name,role_remark,create_time,create_user,update_time,update_user FROM sys_role");
		
		StringBuffer sqlCase = new StringBuffer();
		
		String lp = " where ";
		List<Object> argsList = new ArrayList<Object>();
		if(null != searchParams){
			if(null != searchParams.get("roleName") && !"".equals((String)searchParams.get("roleName"))){
				sqlCase.append(lp).append(" role_name like ? ");
				argsList.add("%" + searchParams.get("roleName") + "%");
				lp = " and ";
			}
		}
		sql.append(sqlCase.toString());
		Object[] args = argsList.toArray();
		List<SysRole> list = dao.query(pageSQL(sql.toString(),page),args,new SysRoleRowmapper());
		page.setTotalItem(findRecordCount("select count(id) as cNum from sys_role " + sqlCase.toString(),args));
		return list;
	}
	
	public List<SysRole> find() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,role_name,role_remark,create_time,create_user,update_time,update_user FROM sys_role");
		sql.append(" where id<>'-1' ");
		List<SysRole> list = dao.query(sql.toString(),new SysRoleRowmapper());
		return list;
	}

	@Override
	public int save(SysRole obj) {
		 
		 StringBuffer sql = new StringBuffer(); 
		 sql.append("insert into sys_role ( "); 
		 sql.append("role_name,role_remark,create_time,create_user,update_user ");  
		 sql.append(" ) values(?,?,?,?,?) "); 
		 Object[] args = {obj.getRoleName(),obj.getRoleRemark(),obj.getCreateTime(),obj.getUpdateUser().getId(),obj.getUpdateUser().getId() };
		 
		 dao.update(sql.toString(), args);
		return dao.queryForInt("SELECT LAST_INSERT_ID()"); 
	}

	@Override
	public SysRole findForObject(Integer id) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,role_name,role_remark,create_time,create_user,update_time,update_user FROM sys_role ");
		sql.append(" where id=? ");
		
		Object[] args = {id};
		return dao.queryForObject(sql.toString(),args,new SysRoleRowmapper());
	}

	@Override
	public void update(SysRole obj) {
		 
 StringBuffer sql = new StringBuffer(); 
 sql.append("update sys_role "); 
 sql.append("set  "); 
 sql.append("role_name=?,role_remark=?,update_user=? where id=?  "); 
 Object[] args = {obj.getRoleName(),obj.getRoleRemark(),obj.getUpdateUser(),obj.getId() };
		 
		 dao.update(sql.toString(), args);
	}

	@Override
	public void delete(Integer id) {
		String sql = "delete from sys_role where id=?";
		dao.update(sql, new Object[]{id});
	}

	@Override
	public boolean findIsExist(String name) {
		return false;
	}
	
	private class SysRoleRowmapper implements RowMapper<SysRole> {
		@Override
		public SysRole mapRow(ResultSet rs, int arg1) throws SQLException {
			SysRole obj = new SysRole();
			obj.setId(rs.getInt("id")); 
			obj.setUpdateUserId(rs.getString("update_user")); 
			obj.setUpdateTime(rs.getString("update_time")); 
			obj.setCreateTime(rs.getString("create_time")); 
			obj.setCreateUserId(rs.getString("create_user")); 
			obj.setRoleRemark(rs.getString("role_remark")); 
			obj.setRoleName(rs.getString("role_name")); 

			return obj;
		}
	}

}
